package com.ctgu.tengyun.controller.saleorder;

import com.ctgu.tengyun.bean.convert.SaleOrderConvert;
import com.ctgu.tengyun.bean.response.SaleOrderProductInfo;
import com.ctgu.tengyun.bean.response.SaleProductListResponse;
import com.ctgu.tengyun.domain.ProductInfo;
import com.ctgu.tengyun.domain.SaleOrderProductRecord;
import com.ctgu.tengyun.service.product.ProductInfoService;
import com.ctgu.tengyun.service.saleorder.SaleOrderProductRecordService;
import com.ctgu.tengyun.util.*;
import com.google.common.collect.ImmutableMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by liaoweizhi on 2019/8/8.
 */
@RestController
@RequestMapping("/v1/sale_order/product")
public class SaleOrderProductController {

    @Resource
    private ProductInfoService productInfoService;
    @Resource
    private SaleOrderProductRecordService saleOrderProductRecordService;

    @RequestMapping("/save_or_update")
    public Object sparePartApply(HttpServletRequest httpServletRequest, @RequestBody SaleOrderProductRecord request) {
        Long userId = RequestExtract.getUserId(httpServletRequest);
        Validator.assertHasFields(!StringUtils.isEmpty(request.getProductNo()), "productNo is required.");

        String productNo = request.getProductNo();
        ProductInfo productInfo = productInfoService.selectByProductNo(productNo);
        Validator.assertResourceFound(null != productInfo, "product info not found, productNo:" + productNo);

        SaleOrderProductRecord updateRecord = new SaleOrderProductRecord();
        updateRecord.setId(request.getId());
        updateRecord.setName(productInfo.getName());
        updateRecord.setProductNo(request.getProductNo());
        updateRecord.setRemark(request.getRemark());
        updateRecord.setSaleDate(request.getSaleDate());
        updateRecord.setServiceDeadline(request.getServiceDeadline());
        updateRecord.setOperationUserId(userId);


        if (null != updateRecord.getId()) {
            SaleOrderProductRecord record = saleOrderProductRecordService.selectByPrimaryKey(request.getId());
            Validator.assertResourceFound(null != record, "sale order product record not found, id:" + request.getId());
            updateRecord.setVersion(record.getVersion());
            saleOrderProductRecordService.updateByPrimaryKeySelective(updateRecord);
        } else {
            saleOrderProductRecordService.insertSelective(updateRecord);
        }

        return ImmutableMap.of("id", updateRecord.getId());
    }

    @GetMapping(value = "list")
    public SaleProductListResponse listInfo(HttpServletRequest request) {
        Integer pageNo = ParameterTool.getParameterInteger(request, "pageNo", 1);
        Integer pageSize = ParameterTool.getParameterInteger(request, "pageSize", 10);
        String productNo = ParameterTool.getParameterString(request, "productNo", "");
        Long saleDateStart = ParameterTool.getParameterLong(request, "saleDateStart", null);
        Long saleDateEnd = ParameterTool.getParameterLong(request, "saleDateEnd", null);

        Validator.validateGt(pageNo, 0);
        Validator.validateBetweenIn(pageSize, 0, 1000);

        Integer totalCount = saleOrderProductRecordService.countSaleProduct(productNo, saleDateStart, saleDateEnd);
        List<SaleOrderProductRecord> recordList = Collections.emptyList();
        if (totalCount > 0) {
            recordList = saleOrderProductRecordService.selectSaleProductList(productNo, saleDateStart, saleDateEnd, pageNo, pageSize);
        }

        Set<String> productNoSet = CollectionsUtil.getComposerIdSet(recordList, SaleOrderProductRecord.PRODUCT_NO_COMPOSER);
        List<ProductInfo> productInfoList = productInfoService.selectByProductNos(productNoSet);
        Map<String, ProductInfo> productNoMap = CollectionsUtil.
                mapComposerId(productInfoList, ProductInfo.PRODUCT_NO_COMPOSER);

        return SaleOrderConvert.toSaleProductListResponse(totalCount, pageSize, recordList, productNoMap);
    }

    @GetMapping(value = "detail/{id}")
    public SaleOrderProductInfo detail(@PathVariable("id") Long id) {
        SaleOrderProductRecord record = saleOrderProductRecordService.selectByPrimaryKey(id);
        Validator.assertResourceFound(null != record, "sale order product recort not found, id:" + id);

        ProductInfo productInfo = productInfoService.selectByProductNo(record.getProductNo());

        return SaleOrderConvert.toSaleOrderProductInfo(record, productInfo);
    }
}
